<?php
/*******************************************************************************
 * 获取支付功能
 *******************************************************************************/
function get_payments($mark = ''){
	cache_delete('payments.php');
	
	$_payments = array('time' => TIME, 'list' => array());
	// 读取插件文件
	$cfg_files = array();
	$payments_dir = @opendir(PAY_PTH);
	if($payments_dir){
		while(($dir = readdir($payments_dir)) !== false){
			if(substr($dir, 0, 1) == '.') continue;
			$payment_pth = PAY_PTH.$dir.'/';
			if(is_dir($payment_pth)){
				$cfg_file = $payment_pth.'_config.php';
				if(file_exists($cfg_file)){
					// $cfg_data = get_config_data($cfg_file);
					$cfg_data = include_once $cfg_file;
					if($cfg_data){
						$cfg_data['dir'] = $dir;
						$cfg_data['path'] = trim(array_pop(explode('/app/', $payment_pth)));
						$_payments['list'][$cfg_data['mark']] = $cfg_data;
					}
				}
			}
		
		// 根目录下暂时不允许有
		}
		closedir($payments_dir);
	}
	cache_write('payment_files.php', $_payments);
	return $_payments;
}

/*function get_config_data($cfg_file){
	$file_data = file_get_contents($cfg_file);
	preg_match('/\/\*\*\s+#CC#\s+[\S\s]+?\*\//', $file_data, $match);
	if(empty($match)) return false;

	$cofnig = str_replace('\\~', '#wave#', $match[0]);
	$headers = array('mark' => '', 'class' => '', 'method' => '');
	foreach($headers as $key => $val){
		preg_match("/\s*~\s*{$key}\s*[：:]\s*([^~]+)/", $cofnig, $submatch);
		if(!empty($submatch)){
			$submatch[1] = trim($submatch[1]);
			$submatch[1] = preg_replace('/\*\/$/', '', $submatch[1]);
			$submatch[1] = preg_replace('/\s+$/', '', $submatch[1]);
			$headers[$key] = str_replace('#wave#', '~', $submatch[1]);
		}
	}
	if(!empty($headers['class'])){
		$cls = $headers['class'];
	}else{
		$cls = 'Config_'.$headers['mark'];
	}
	if(!empty($headers['method'])){
		$method = $headers['method'];
	}else{
		$method = 'get_config';
	}

	try{
		include_once $cfg_file;
		if(class_exists($cls) && method_exists($cls, $method)){
			//eval("\$cfg_data={$cls}::get_config();");
			$cfg_data = call_user_func_array(array($cls, $method), array());
		}
	}catch(Exception $e){return false;}
	return $cfg_data;
}*/
/*******************************************************************************
 * 激活支付功能
 *******************************************************************************/
function activate_payment($marks){
	if(!$_payments = cache_read('payment_files.php')){
		return false;
	}
	if(!is_array($marks))$marks = array();
	$cfg_files = $_payments['list'];
	foreach($cfg_files as $mark => $cfg_data){
		if(in_array($mark, $marks)){
			$activate = 1;
		}else{
			$activate = 0;
		}
		try{
			$cfg_data['state'] = 1;
		}catch(Exception $e){}
		$_payments['list'][$mark] = $cfg_data;
	}
	$_payments['time'] = TIME;
	return cache_write('payment_files.php', $_payments);
}
?>